package com.cci.kangdao.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;

/**
 * GM申请记录dao
 *
 */
@MapperScan
public interface GMApplyRecordTDao {

	//根据用户id查询所有GM申请记录集合
	@Select("select ID as recordId,ApplyTime as applyTime,Status as status,ExpireDate as expireDate from GMApplyRecord_T \n" +
			"where Creator = #{userId} and Flag = 0 and ApplySource = 0 order by ApplyTime desc")
	List<Map<String,Object>> getGMRecordList(long userId);
	
	
	//根据用户id查询所有GM申请记录集合
	@Select("select count(1) from GMApplyRecord_T where UserId = #{userId}")
	long countUserRecord(@Param("userId") Long userId);
	
	
	

	//根据GM申请记录主键id获取详情
	@Select("select a.ID as recordId,b.Contact as userName,b.Phone userPhone,a.CompanyNo as companyNo,a.CompanyName as companyName,\n" +
			"case when b.Type = 2 then b.UserName else '' end as wwid,\n" +
			"a.ServiceType as serviceType,a.ApplyTime as applyTime,a.Status as statusInt,d.DValue as statusValue,ISNULL(a.WithdrawTime,'') as withdrawTime,\n" +
			"ISNULL(a.AuthorizeTime,'') as authorizeTime,ISNULL(a.RefuseTime,'') as refuseTime,ISNULL(a.RefuseReason,'') as refuseReason,\n" +
			"ISNULL(a.DeauthorizeTime,'') as deauthorizeTime,ISNULL(a.DeauthorizeReason,'') as deauthorizeReason,e.ServiceToolOrderNo as orderNo,\n" +
			"f.Contact as hanglerName,a.ExpireDate as expireDate,g.Contact as updateName,\n" +
			"case when h.IsType = 1 then 1 else 0 end as userType\n" +
			"from GMApplyRecord_T a\n" +
			"inner join User_T b on a.UserId = b.ID and b.Flag = 0\n" +
			"inner join Dictionaries_T d on a.Status = d.DInt and d.DTableName = 'GMUserApplyStatus' and d.Flag = 0\n" +
			"left join ServiceToolOrder_T e on a.OrderId = e.ID and e.Flag = 0\n" +
			"left join User_T f on a.HandlerId = f.ID and f.Flag = 0\n" +
			"left join User_T g on a.Updator = g.ID and g.Flag = 0\n" +
			"left join Usergroup_T h on b.UsergroupID = h.ID and h.Flag = 0\n" +
			"where a.ID = #{recordId}  and a.Flag = 0")
	Map<String,Object> getGMApplyDetail(long recordId);

	int updateGMApplyRecord(Map<String,Object> map);

	//根据申请记录id获取申请记录申请人和审核人相关信息
	@Select("select b.ID as applyUserId,b.UserName as applyUserNames,b.Contact as applyUserName,b.Phone as applyUserPhone,\n" +
			"d.ID as auditUserId,d.Phone as auditUserPhone,d.UserName as auditUserName from GMApplyRecord_T a\n" +
			"inner join User_T b on a.UserId = b.ID and b.Flag = 0\n" +
			"left join GMAuditRecord_T c on a.ID = c.RecordId and c.Flag = 0\n" +
			"left join User_T d on c.HandlerId = d.ID and d.Flag = 0\n" +
			"where a.ID = #{recordId} and a.Flag = 0")
	List<Map<String,Object>> getRemindAuditMsg(long recordId);


	int insertGMNotifyRecordData(Map<String,Object> map);


	@Select(" <script> select b.ID as recordId,c.Contact as userName,c.Phone as userPhone,ISNULL(c.WWID,'') as wwid,\n" +
            "b.CompanyName as companyName,b.ApplyTime as applyTime,DInt as statusInt,d.DValue as statusValue,\n" +
            "case when f.IsType = 1 then 1 else 0 end as userType,b.OrderId as orderId\n" +
            "from GMAuditRecord_T a\n" +
            "inner join GMApplyRecord_T b on a.RecordId = b.ID and b.Flag = 0 and b.ApplySource = 0\n" +
            "inner join User_T c on b.UserId = c.ID and c.Flag = 0\n" +
            "inner join Dictionaries_T d on b.Status = d.DInt and d.DTableName = 'GMUserApplyStatus' and d.Flag = 0\n" +
            "inner join Usergroup_T f on c.UsergroupID = f.ID and f.Flag = 0\n" +
            "where a.Flag = 0 and b.Status in <foreach collection='status' item='item' open='(' separator=',' close=')'>#{item} </foreach> and a.HandlerId = #{handlerId} order by b.ID desc </script>")
    List<Map<String, Object>> getGMApplyList(@Param("status") List<String> status, @Param("handlerId") long handlerId);


	//获取登录人未读的GM工单消息提醒总数
	@Select("select count(ID) from GMNotifyRecord_T where IsRead = 0 and ReceiverId = #{receiverId} and Flag = 0")
	int getGMUnReadNum(long receiverId);

	//通过组织类型和组织id获取insert订单集合
	@Select("select ID as orderId,ServiceToolOrderNo as orderNo,UsableGMNum as availableNum,AllGMNum as totalNum, ExpirationTime as expireDate from ServiceToolOrder_T\n" +
			"where Flag = 0 and OrganizationType = #{organizationType} and OrganizationId = #{organizationId} and ServiceToolOrderStatus = 1 and UsableGMNum > 0 order by ID desc")
	List<Map<String,Object>> getServiceToolOrderListByOrganizationTypeOrganizationId(@Param("organizationType")int organizationType,@Param("organizationId")long organizationId);

	@Select("select ID as id,Type as msgType,MsgContent as msgContent,SendTime as sendTime,IsRead as isRead from GMNotifyRecord_T\n" +
			"where Flag = 0 and ReceiverId = #{receiverId} and SendType in(0,2) order by ID desc")
	List<Map<String,Object>> getGMMsgList(long receiverId);

	@Update("update GMNotifyRecord_T set IsRead = 1,Updator=#{Updator},UpdateTime=#{UpdateTime} where receiverId = #{receiverId} and IsRead = 0")
	int updateIsRead(Map<String, Object> map);

	@Select("select COUNT(1) from ServiceToolOrder_T where Flag = 0 and OrganizationType = #{organizationType} and OrganizationId = #{organizationId} and ServiceToolOrderStatus = 1 and UsableGMNum > 0")
	int getUsableGMNum(@Param("organizationType")int organizationType,@Param("organizationId")long organizationId);

	@Select("select COUNT(1) from ServiceToolOrder_T where Flag = 0 and OrganizationType = #{organizationType} and OrganizationId = #{organizationId} and ServiceToolOrderStatus = 1")
	int getUsableServiceToolOrderNum(@Param("organizationType")int organizationType,@Param("organizationId")long organizationId);

	@Update("update GMNotifyRecord_T set IsRead = 1 where Id = #{ID}")
	int updateGMMsgReadById(Map<String, Object> map);

	//GMApplyRecord一键已读
	@Update("update GMNotifyRecord_T set IsRead = 1 ,Updator = #{Updator}, UpdateTime = #{UpdateTime} where IsRead = 0 and ReceiverId = #{ReceiverId}")
    int updateNotifyReadStatusByReceiverId(@Param("ReceiverId") Long ReceiverId,@Param("Updator") Long updator,@Param("UpdateTime") String updateTime);

	//根据id 更新为已读
	@Update("update GMNotifyRecord_T set IsRead = 1 ,Updator = #{Updator}, UpdateTime = #{UpdateTime} where ID = #{ID}")
	int updateGMMsgIsReadById(@Param("ID") Integer ID,@Param("Updator") Long updator,@Param("UpdateTime") String updateTime);
	
	int insertSelective(Map<String, Object> applyRecord);
	
	@Select("select top 1 ID,UserId,ServiceType,ExpireDate, IAExpireDate, SRVExpireDate, CMPExpireDate, ServiceUptodateType,BTGFExpireDate from GMApplyRecord_T where UserId = #{userId} and Status = 2 and Flag = 0 and RecordType = 0")
	Map<String,Object> getUserGMLevelByUserId(@Param("userId") Long userId);
	
	int insertSelectiveNew(Map<String, Object> map);
	
	/**
	 * 根据ID以及版本修改申请记录的状态
	 */
	@Update("update GMApplyRecord_T set Status = #{status}, WithdrawTime = #{withdrawTime}, Updator = #{updator}, UpdateTime = #{updateTime} Where ID = #{recordId} and version = #{version}")
	int updateApplyRecordStatus(Map<String, Object> map);
	
	
	List<Map<String, Object>> getGMRecordListNew(@Param("userId") Long userId);
	
	// 申请记录详情
	Map<String, Object> applyRecordDetail(@Param("id") Long id);
	
	List<Map<String, Object>> stationAuditList(@Param("serviceStationID") Long serviceStationId, @Param("recordStatusList") List<Integer> recordStatusList, @Param("type") Integer type);


	int updateApplyRecordByIdAndVersion(Map<String, Object> map);
	
	int updateApplyRecordServiceTypeByIdAndVersion(Map<String, Object> map);
	
	
	// 获取某个用户审核中的权限单
	List<Map<String, Object>> getAuditApplyRecordsByUserId(@Param("userId") Long userId);

	
	int doWithdrawApplyRecordById(Map<String, Object> map);
	
	
}
